package com.wzvtc.vsl.securitytool;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.wzvtc.vsl.securitytool.ui.SettingItemView;


/**
 * Created by Vsl on 2015/3/25.
 */
public class Setup2Activity extends Activity{
    private SettingItemView siv_setup2_sim;
    private SharedPreferences sp;
    private TelephonyManager tm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup2);

        siv_setup2_sim = (SettingItemView) findViewById(R.id.siv_setup2_sim);
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        sp = getSharedPreferences("config", MODE_PRIVATE);

        siv_setup2_sim.setTitle("绑定sim卡");
        if (!TextUtils.isEmpty(sp.getString("sim", null))) {
            siv_setup2_sim.setChecked(true);
            siv_setup2_sim.setDesc("sim卡已经绑定");
        } else {
            siv_setup2_sim.setChecked(false);
            siv_setup2_sim.setDesc("sim卡没有绑定");
        }

        siv_setup2_sim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                if (siv_setup2_sim.isChecked()) {
                    editor.putString("sim", null);

                    siv_setup2_sim.setChecked(false);
                    siv_setup2_sim.setDesc("sim卡没有绑定");
                } else {
                    String sim = tm.getSimSerialNumber();
                    editor.putString("sim", sim);

                    siv_setup2_sim.setChecked(true);
                    siv_setup2_sim.setDesc("sim卡已经绑定");
                }
                editor.commit();
            }
        });
    }

    public void next(View view) {
        String sim = sp.getString("sim", null);
        if (TextUtils.isEmpty(sim)) {
            Toast.makeText(this, "sim卡未绑定", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(Setup2Activity.this, Setup3Activity.class);
        startActivity(intent);
        finish();
    }

    public void pre(View view) {
        Intent intent = new Intent(Setup2Activity.this, Setup1Activity.class);
        startActivity(intent);
        finish();
    }
}
